package models

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
)

var db *gorm.DB
var Redis *redis.Client
var err error

func init() {
	username, _ := beego.AppConfig.String("mysql::Username")
	password, _ := beego.AppConfig.String("mysql::Password")
	host, _ := beego.AppConfig.String("mysql::Host")
	port, _ := beego.AppConfig.String("mysql::Port")
	redisHost, _ := beego.AppConfig.String("redis::Host")
	redisPort, _ := beego.AppConfig.String("redis::Port")
	log.Println(username, password, host, port, "123123")
	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	dsn := username + ":" + password + "@tcp(" + host + ":" + port + ")/demo?charset=utf8mb4&parseTime=True&loc=Local"
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		log.Println("连接数据库失败", err)
		return
	}
	err = db.AutoMigrate(new(BroudCast3), new(LookCast), new(AnchorList), new(Barrage3))
	if err != nil {
		log.Println("数据表建立失败", err)
		return
	}
	Redis = redis.NewClient(&redis.Options{
		Addr: redisHost + ":" + redisPort,
		DB:   0,
	})
}
